<script setup>
import { ref, watch } from 'vue';

// 接收 imageList 属性
defineProps({
  imageList: {
    type: Array,
    default: () => [
      "https://fengfeng-web-ai.oss-cn-beijing.aliyuncs.com/lv-project/shoe2.png",
      "https://fengfeng-web-ai.oss-cn-beijing.aliyuncs.com/lv-project/shoe3.png",
      "https://fengfeng-web-ai.oss-cn-beijing.aliyuncs.com/lv-project/shoe4.png",
      "https://fengfeng-web-ai.oss-cn-beijing.aliyuncs.com/lv-project/shoe5.jpg",
      "https://fengfeng-web-ai.oss-cn-beijing.aliyuncs.com/lv-project/shoe6.png",
      "https://fengfeng-web-ai.oss-cn-beijing.aliyuncs.com/lv-project/shoe7.png"
    ]
  }
})

// 小图切换大图
const activeIndex = ref(0)

// 控制滑块移动
const target = ref(null);
import { useMouseInElement } from '@vueuse/core'
const { elementX, elementY, isOutside } = useMouseInElement(target)
const top = ref(0)
const left = ref(0)
watch([elementX, elementY, isOutside], () => {
  if (!isOutside.value) {
    if (elementX.value > 100 && elementX.value < 300) left.value = elementX.value - 100;
    if (elementY.value > 100 && elementY.value < 300) top.value = elementY.value - 100;

    if (elementX.value > 300) left.value = 200;
    if (elementX.value < 100) left.value = 0;
    if (elementY.value > 300) top.value = 200;
    if (elementY.value < 100) top.value = 0;
  }
});
</script>

<template>
  <div class="goods-image">
    <!-- 左侧大图 -->
    <div class="middle" ref="target">
      <img :src="imageList[activeIndex]" alt="" />
      <div class="layer" :style="{ left: `${left}px`, top: `${top}px` }"></div>
    </div>
    <!-- 小图列表 -->
    <ul class="small">
      <li v-for="(img, i) in imageList" :key="i" @mouseenter="activeIndex = i">
        <img :src="img" alt="" />
      </li>
    </ul>
    <!-- 放大镜大图 -->
    <div class="large" :style="[
      {
        backgroundImage: `url(${imageList[0]})`,
        backgroundPositionX: `0px`,
        backgroundPositionY: `0px`,
      },
    ]" v-show="false"></div>
  </div>
</template>

<style scoped lang="scss">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .layer {
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 0, 0.2);
    left: 0;
    top: 0;
    position: absolute;
  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &:hover,
      &.active {
        border: 2px solid $xtxColor;
      }
    }
  }
}

.hot-products-section {
  width: 280px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 15px;
  height: 100%;

  .hot-header {
    padding: 10px 0;
    border-bottom: 1px solid #eee;
    margin-bottom: 10px;

    .hot-title {
      font-size: 18px;
      font-weight: bold;
      color: #e02e41;
    }

    .hot-subtitle {
      font-size: 12px;
      color: #999;
      margin-left: 8px;
    }
  }

  .hot-list {
    .hot-item {
      display: flex;
      align-items: center;
      padding: 12px 0;
      border-bottom: 1px dashed #eee;
      cursor: pointer;
      transition: all 0.3s;

      &:hover {
        background: #fff9f9;
        transform: translateX(-5px);
      }

      .rank {
        width: 24px;
        height: 24px;
        background: #f5f5f5;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        font-weight: bold;
        margin-right: 10px;

        &.top-rank {
          background: #e02e41;
          color: white;
        }
      }

      .product-image {
        width: 50px;
        height: 50px;
        border-radius: 4px;
        object-fit: cover;
        margin-right: 10px;
      }

      .product-info {
        flex: 1;

        .product-name {
          font-size: 14px;
          color: #333;
          margin-bottom: 4px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .product-price {
          color: #e02e41;
          font-weight: bold;
          font-size: 15px;
        }

        .product-sales {
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
}
</style>